package com.jwthhealth_pub.common.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import com.jwthhealth_pub.R;

/**
 * 健康页面得分仪表
 * Created by liuzhen on 2017/12/8.
 */

public class JwthHealthMeter extends View {

    private int mTextSize = 100;

    private String mScore = "0";

    public void setmScore(String mScore) {
        this.mScore = mScore;
        invalidate();
    }

    // TODO: 2017/12/8 有几种文字就要几种paint
    private Paint mCirclePaint, mTextScorePaint, mTextScoreDescribePaint;

    public JwthHealthMeter(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);

        mCirclePaint = new Paint();
        mCirclePaint.setStrokeWidth(3);
        mCirclePaint.setColor(getResources().getColor(R.color.primarycolor));
        mCirclePaint.setAntiAlias(true);
        mCirclePaint.setStyle(Paint.Style.STROKE);

        mTextScorePaint = new Paint();
        mTextScorePaint.setStrokeWidth(3);
        mTextScorePaint.setColor(getResources().getColor(R.color.primarycolor));
        mTextScorePaint.setAntiAlias(true);
        mTextScorePaint.setTextSize((float) (mTextSize / 1.5));

        mTextScoreDescribePaint = new Paint();
        mTextScoreDescribePaint = new Paint();
        mTextScoreDescribePaint.setStrokeWidth(3);
        mTextScoreDescribePaint.setColor(getResources().getColor(R.color.primarycolor));
        mTextScoreDescribePaint.setAntiAlias(true);
        mTextScoreDescribePaint.setTextSize(mTextSize / 3);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        int height = getHeight();
        int width = getWidth();

        Log.d("JwthHealthMeter", "height:" + height);
        Log.d("JwthHealthMeter", "width:" + width);

        int radius = 400;

        //文字
        canvas.drawText(mScore, width / 2 - mTextSize, height / 2 + mTextSize / 4, mTextScorePaint);

        canvas.drawText("分", width / 2 + mTextSize / 2, height / 2 + mTextSize / 4, mTextScoreDescribePaint);

        canvas.drawText("较健康", width / 2 - mTextSize / 2, height / 2 + radius / 5 * 2, mTextScoreDescribePaint);

        //外圆环
        RectF rect = new RectF(width / 2 - radius / 2, height / 2 - radius / 2, width / 2 + radius / 2, height / 2 + radius / 2);
        canvas.drawArc(rect, 150, 240, false, mCirclePaint);

        //刻度
        canvas.drawLine(width / 2, height / 2 - radius / 2 + 40, width / 2, height / 2 - radius / 2 + 20, mCirclePaint);
        int lineCount = 240 / 6;
        for (int i = 0; i < lineCount / 2; i++) {
            canvas.save();
            canvas.rotate(6, width / 2, height / 2);
            canvas.drawLine(width / 2, height / 2 - radius / 2 + 40, width / 2, height / 2 - radius / 2 + 20, mCirclePaint);
        }

        canvas.rotate(-6 * lineCount / 2, width / 2, height / 2);

        for (int i = 0; i < lineCount / 2; i++) {
            canvas.save();
            canvas.rotate(-6, width / 2, height / 2);
            canvas.drawLine(width / 2, height / 2 - radius / 2 + 40, width / 2, height / 2 - radius / 2 + 20, mCirclePaint);
        }

        canvas.rotate(6 * lineCount / 2, width / 2, height / 2);
    }
}
